package com.hdax.dm.service;

import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.dto.scheduler.SchedulerPriceDto;
import com.hdax.dm.dto.scheduler.TimePlanDto;
import com.hdax.dm.entity.scheduler.DmScheduler;
import com.hdax.dm.entity.scheduler.DmSchedulerSeatPrice;
import com.hdax.dm.feign.scheduler.DmSchedulerFeign;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DmSchedulerService {

    @Resource
    private DmSchedulerFeign dmSchedulerFeign;

    //获取到商品的排期信息
    public CommonResponse<List<TimePlanDto>> getTimePlan(Long itemId){
        //拷贝属性
        List<DmScheduler> dmSchedulers = dmSchedulerFeign.getSchedulersByItem(itemId);
        List<TimePlanDto> timePlanDtos = new ArrayList<>();
        dmSchedulers.forEach(dmScheduler -> {
            TimePlanDto timePlanDto = new TimePlanDto();
            BeanUtils.copyProperties(dmScheduler,timePlanDto);
            timePlanDtos.add(timePlanDto);
        });
        return DtoUtil.returnSuccess(timePlanDtos);
    }

    //获取到排期的价格信息
    public CommonResponse<List<SchedulerPriceDto>> getPrice(Long scheduleId){
        //拷贝属性
        List<DmSchedulerSeatPrice> list = dmSchedulerFeign.getPrice(scheduleId);
        List<SchedulerPriceDto> dtos = new ArrayList<>();
        list.forEach(dmSchedulerSeatPrice -> {
            SchedulerPriceDto schedulerPriceDto = new SchedulerPriceDto();
            BeanUtils.copyProperties(dmSchedulerSeatPrice,schedulerPriceDto);
            Long seatCount = dmSchedulerFeign.seatCount(scheduleId,dmSchedulerSeatPrice.getAreaLevel());
            schedulerPriceDto.setIsHaveSeat(dmSchedulerSeatPrice.getSeatNum()!=seatCount);
            dtos.add(schedulerPriceDto);
        });
        return DtoUtil.returnSuccess(dtos);
    }
}
